<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <!-- <el-form-item label="分类名称" prop="typeName">
        <el-select v-model="queryParams.typeName" placeholder="请选择仓库"  @keyup.enter.native="handleQuery">
          <el-option :label='item.typeName' :value='item.typeName' v-for="item in typeSearch"></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="分类名称" prop="typeAllName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入备品备件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sparepart:types:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sparepart:types:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="typesList"
      row-key="typeId"
      node-key="typeId"
      :default-expanded-keys="idArr"
      height="700"
      :tree-props="{children: 'children',hasChildren: 'hasChildren'}">
      <el-table-column label="分类名称" align="left" prop="typeName" />
      <el-table-column label="排序" align="center" prop="typeOrder"/>
      <el-table-column label="备注" align="center" prop="typeRemark" />
      <el-table-column label="维护人" align="center" prop="sysUser.nickName" />
      <el-table-column label="维护时间" align="center" prop="typeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.typeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:dept:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sparepart:types:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sparepart:types:remove']"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>


    <!-- 添加或修改备品备件货品分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>

          <el-col :span="12">
            <el-form-item label="分类名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入分类名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="typeOrder">
              <el-input-number v-model="form.typeOrder" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.typeParentid !== 0">
            <el-form-item label="上级分类" prop="typeParentid">
              <treeselect v-model="form.typeParentid"
                          :options="typesOptions"
                          :normalizer="normalizer"
                          placeholder="选择上级分类/默认顶级"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="typeRemark">
              <el-input type="textarea"  v-model="form.typeRemark" controls-position="right"  />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listTypeExcludeChild,listTypes, getTypes, delTypes, addTypes, updateTypes, exportTypes } from "@/api/sparepart/types";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Types",
  components: { Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //搜索/弹出框下拉款
      typeSearch:[],
      // 备品备件货品分类表格数据
      typesList: [],
      //弹出框数结构
      typesOptions:[],
      // 弹出层标题
      title: "",
      //展开树的子字节
      idArr:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        typeName: null,
        typeAllName: null,
        typeRemark: null,
        typeParentid: null,
        typeOrder: null,
        typeIsdel: null,
        userId: null,
        typeTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeParentid: [
          { required: true, message: "上级分类不能为空", trigger: "blur" }
        ],
        typeName:[
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询备品备件货品分类列表 */
    getList() {
      this.loading = true;
      listTypes(this.queryParams).then(response => {
        this.typesList =this.handleTree(response.data, "typeId","typeParentid");
        this.typesList.forEach(m=>{
          this.idArr.push(m.typeId)
        })
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.typeId,
        label: node.typeName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: null,
        typeName: null,
        typeAllName: null,
        typeRemark: null,
        typeParentid: null,
        typeOrder: null,
        typeIsdel: null,
        userId: null,
        typeTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.typeParentid = row.typeId;
      }
      this.open = true;
      this.title = "添加备品备件货品分类";
      listTypes(this.queryParams).then(response => {
        this.typesOptions =this.handleTree(response.data, "typeId","typeParentid");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const typeId = row.typeId || this.ids
      getTypes(typeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改备品备件货品分类";
      });
      listTypeExcludeChild(row.typeId).then(response => {
        this.typesOptions =this.handleTree(response.data, "typeId","typeParentid");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.typeAllName = this.form.typeParentid
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.typeId != null) {
            updateTypes(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTypes(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除备品备件货品分类为"' + row.typeName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTypes(row.typeId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有备品备件货品分类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTypes(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
